package com.mobile.learning;

import java.io.File;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.IntentAction;
import com.mobile.learning.lib.DBClass;
import com.mobile.learning.lib.DataSoal;
import com.mobile.learning.lib.Jenis;
import com.mobile.learning.lib.MCrypt;
import com.mobile.learning.lib.Soal;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

public class TryoutSoalActivity extends Activity {

	DBClass db = new DBClass(this);
	String value1 = "";
	String value2 = "";
	String value3 = "";
	String value4 = "";
	int Total, score, idjenis, no_soal;
	String ke;
	long elTime;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_task_latihan);
		Bundle extras = getIntent().getExtras();
		idjenis = extras.getInt("jenis");
		ke = extras.getString("index_soal");
		no_soal = extras.getInt("no_soal");
		elTime = extras.getLong("elTime");
		score = extras.getInt("score");
		no_soal++;
		Total = score;
		Log.d("nilai", String.valueOf(Total));
		DataSoal ds = db.getSoal(idjenis, Integer.parseInt(ke));
		Log.d("ke", ke);
		TextView nomor = (TextView) findViewById(R.id.no_soal);
		TextView soal = (TextView) findViewById(R.id.t_soal);

		RadioButton jawaban_a = (RadioButton) findViewById(R.id.jawab_a);
		RadioButton jawaban_b = (RadioButton) findViewById(R.id.jawab_b);
		RadioButton jawaban_c = (RadioButton) findViewById(R.id.jawab_c);
		RadioButton jawaban_d = (RadioButton) findViewById(R.id.jawab_d);
		Chronometer timer = (Chronometer) findViewById(R.id.chronometer1);
		ImageView img_soal = (ImageView) findViewById(R.id.img_soal);

		// timer
		if (no_soal == 1) {
			elTime = SystemClock.elapsedRealtime();
		}

		int stopedMillisecond = 0;
		String array[] = timer.getText().toString().split(":");
		stopedMillisecond = Integer.parseInt(array[0]) * 60 * 1000
				+ Integer.parseInt(array[1]) * 1000;
		timer.setBase(elTime - stopedMillisecond);
		timer.start();

		Jenis j = db.getJenis(idjenis);

		soal.setText(ds.getText_soal());
		jawaban_a.setText(ds.getText_jawaban_a());
		jawaban_b.setText(ds.getText_jawaban_b());
		jawaban_c.setText(ds.getText_jawaban_c());
		jawaban_d.setText(ds.getText_jawaban_d());

		value1 = ds.getVal_a();
		value2 = ds.getVal_b();
		value3 = ds.getVal_c();
		value4 = ds.getVal_d();

		Log.d("val", value1 + value2 + value3 + value4);

		// SET IF IMAGES EXIST//
		MCrypt mc = new MCrypt();
		if (!ds.getGambarsoal().equals("null")) {
			try {
				File f = new File("/mnt/sdcard/mobilelearning/"
						+ mc.bytesToHex(mc.encrypt(ds.getGambarsoal())));
				if (f.exists())
					img_soal.setImageBitmap(BitmapFactory.decodeFile(f
							.getAbsolutePath()));
				Log.d("drawable", String.valueOf(Drawable.createFromPath(f
						.getAbsolutePath())));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (!ds.getGambarjawaban_a().equals("null")) {
			try {
				File f = new File("/mnt/sdcard/mobilelearning/"
						+ mc.bytesToHex(mc.encrypt(ds.getGambarjawaban_a())));

				if (f.exists())
					jawaban_a.setCompoundDrawablesWithIntrinsicBounds(null,
							null, Drawable.createFromPath(f.getAbsolutePath()),
							null);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (!ds.getGambarjawaban_b().equals("null")) {
			try {
				File f = new File("/mnt/sdcard/mobilelearning/"
						+ mc.bytesToHex(mc.encrypt(ds.getGambarjawaban_b())));
				if (f.exists())
					jawaban_b.setCompoundDrawablesWithIntrinsicBounds(null,
							null, Drawable.createFromPath(f.getAbsolutePath()),
							null);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		if (!ds.getGambarjawaban_c().equals("null")) {
			try {
				File f = new File("/mnt/sdcard/mobilelearning/"
						+ mc.bytesToHex(mc.encrypt(ds.getGambarjawaban_c())));
				if (f.exists())
					jawaban_c.setCompoundDrawablesWithIntrinsicBounds(null,
							null, Drawable.createFromPath(f.getAbsolutePath()),
							null);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (!ds.getGambarjawaban_d().equals("null")) {
			try {
				File f = new File("/mnt/sdcard/mobilelearning/"
						+ mc.bytesToHex(mc.encrypt(ds.getGambarjawaban_d())));
				if (f.exists())
					jawaban_d.setCompoundDrawablesWithIntrinsicBounds(null,
							null, Drawable.createFromPath(f.getAbsolutePath()),
							null);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar_post);
		actionBar.setTitle(j.getNama_jenis());
		nomor.setText(no_soal + ". ");

	}

	public void next(View v) {
		Soal s = db.getJumlahSoal(idjenis);
if (no_soal >= s.getJml_soal()) {
			Intent j = new Intent(getBaseContext(), NilaiActivity.class);
			j.putExtra("score", Total);
			j.putExtra("elTime", elTime);
			j.putExtra("jenis", idjenis);
			startActivity(j);
			this.finish();
		} else {
			Intent i = new Intent(getBaseContext(), TryoutSoalActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			i.putExtra("index_soal", String.valueOf(Integer.parseInt(ke) + 4));
			i.putExtra("jenis", idjenis);
			i.putExtra("no_soal", no_soal);
			i.putExtra("elTime", elTime);
			i.putExtra("score", Total);
			startActivity(i);
		}
	}

	public void pilihA(View v) {
		if (value1.equals("1"))
			Total = score + 4;
		else
			Total = score + -1;
	}

	public void pilihB(View v) {
		if (value2.equals("1"))
			Total = score + 4;
		else
			Total = score + -1;
	}

	public void pilihC(View v) {
		if (value3.equals("1"))
			Total = score + 4;
		else
			Total = score + -1;
	}

	public void pilihD(View v) {
		if (value4.equals("1"))
			Total = score + 4;
		else
			Total = score + -1;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
					this);
			alertDialogBuilder.setTitle("Peringatan !");
			alertDialogBuilder
					.setMessage("Apakah anda yakin untuk menutup soal ?")
					.setCancelable(true)
					.setIcon(R.drawable.ic_icon_lock)
					.setNegativeButton("No", null)
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									TryoutSoalActivity.this.finish();
								}
							});
			AlertDialog alertDialog = alertDialogBuilder.create();
			alertDialog.show();
		}
		return super.onKeyDown(keyCode, event);
	}
}
